#include "stdinclude.h"

#include "utils/debugtools.h"


//------------------------------------------------------------------------------
/**
	This function is called by _assert() when the assertion fails.
*/
 
void
_assert_fn(const char* exp, const char* file, int line)
{
	if (Console::getSingletonPtr() && Console::getSingletonPtr()->isOpen())
	{
		_error("*** ASSERTION ***\nexpression: %s\nfile: %s\nline: %d\n", exp, file, line);
	}
	else
	{
		String str = StringTools::format("*** ASSERTION ***\nexpression: %s\nfile: %s\nline: %d\n", exp, file, line);
		OsTools::Error(str.c_str());
	}
}

//------------------------------------------------------------------------------
/**
	This function is called by _assert2() when the assertion fails.
*/

 
void 
_assert2_fn(const char* exp, const char* msg, const char* file, int line)
{
	if (Console::getSingletonPtr() && Console::getSingletonPtr()->isOpen())
	{
		_error("***  ASSERTION ***\nprogrammer says: %s\nexpression: %s\nfile: %s\nline: %d\n", msg, exp, file, line);
	}
	
	else
	{
		String str = StringTools::format("***  ASSERTION ***\nprogrammer says: %s\nexpression: %s\nfile: %s\nline: %d\n", msg, exp, file, line);
		OsTools::Error(str.c_str());        
	}
	
}

//------------------------------------------------------------------------------
/**
	This function is called when a serious situation is encountered which
	requires abortion of the application.
*/
void __cdecl
_error(const char* msg, ...)
{
	va_list argList;
	va_start(argList, msg);
	String str = StringTools::format_arg_list(msg, argList);
	
	_error(str);	
	
	va_end(argList);
}
void __cdecl
_error(const String& str)
{
	if (Console::getSingletonPtr() && Console::getSingletonPtr()->isOpen())
	{
		Console::getSingleton().error(str);
	}	
	else
	{		
		OsTools::Error(str.c_str());
	}
}
//------------------------------------------------------------------------------
/**
	This function is called when a warning should be issued which doesn't
	require abortion of the application.
*/
void __cdecl
_warning(const char* msg, ...)
{
	va_list argList;
	va_start(argList, msg);
	String str = StringTools::format_arg_list(msg, argList);

	_warning(str);	
	
	va_end(argList);
}        

void __cdecl
_warning(const String& str)
{
	if (Console::getSingletonPtr() && Console::getSingletonPtr()->isOpen())
	{
		Console::getSingleton().warning(str);
	}
	
	else
	{		
		OsTools::Error(str.c_str());
	}	
}
//------------------------------------------------------------------------------
/**
	's printf replacement. Will redirect the text to the console
	and/or logfile.
*/
void __cdecl
_printf(const char *msg, ...)
{
	va_list argList;
	va_start(argList, msg);
	String str = StringTools::format_arg_list(msg, argList);
	
	_printf(str);
	va_end(argList);
}
void __cdecl
_printf(const String& str)
{
	if (Console::getSingletonPtr() && Console::getSingletonPtr()->isOpen())
	{
		Console::getSingleton().print(str);
	}
}

//------------------------------------------------------------------------------
/**
	Put the message to the debug window.
*/
void __cdecl
_dbgout(const char *msg, ...)
{
	va_list argList;
	va_start(argList,msg);
	String str = StringTools::format_arg_list(msg, argList);
	
	_dbgout(str);

	va_end(argList);
}

void __cdecl
_dbgout(const String& str)
{
	OsTools::DebugOut(str);
}

